/*
 * @author: wizardc
 */

using System;
using Dou.ScreenController;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace DouEditor.ScreenController
{
    [CustomEditor(typeof(HorizontalOrVerticalLayoutGroup), true)]
    [CanEditMultipleObjects]
    public class HorizontalOrVerticalLayoutGroupExtendEditor : Editor
    {
        private Editor _horizontalOrVerticalLayoutGroupEditor;
        private HorizontalOrVerticalLayoutGroup _target;
        
        private void OnEnable()
        {
            var editorType = Type.GetType("UnityEditor.UI.HorizontalOrVerticalLayoutGroupEditor,UnityEditor.UI");
            if (editorType != null && target !=null)
            {
                _horizontalOrVerticalLayoutGroupEditor = CreateEditor(target, editorType);
            }
            _target = (HorizontalOrVerticalLayoutGroup) target;
        }
        
        public override void OnInspectorGUI()
        {
            var color = GUI.color;
            var adapter = _target.GetComponent<HorizontalOrVerticalLayoutGroupAdapter>();
            if (adapter != null)
            {
                ScreenControllerMgr.GetNowLayoutNameAndColor(out var name, out var col);
                GUI.color = col;
                EditorGUILayout.LabelField(name);
            }
            if (_horizontalOrVerticalLayoutGroupEditor != null)
            {
                _horizontalOrVerticalLayoutGroupEditor.OnInspectorGUI();
            }
            GUI.color = color;
        }

        private void OnDisable()
        {
            if (_horizontalOrVerticalLayoutGroupEditor != null)
            {
                DestroyImmediate(_horizontalOrVerticalLayoutGroupEditor);
            }
            _horizontalOrVerticalLayoutGroupEditor = null;
        }
    }
}
